<?php
//Hàm đưa ra tin liên quan
function news_relate($news,$total_record = 10) {
    $list_relate = array();
    //nếu đã có tin liên quan
    if($news['new_relate'] != '') {
        $arr_relate_id = explode(',',$news['new_relate']);
        if($total_record > count($arr_relate_id)) {
            $total_record = count($arr_relate_id);
        }
        for($i = 0; $i < $total_record; $i++) {
            $db_relate = new db_query('SELECT new_id,new_title,new_picture,new_cat_id
                                        FROM news
                                        WHERE new_cat_id = '.$news['new_cat_id'].' AND new_id = '.$arr_relate_id[$i].'
                                        LIMIT 1');
            $relate_item = mysql_fetch_assoc($db_relate->result);unset($db_relate);
            $list_relate[] = $relate_item;
        }
        return $list_relate;
    }
    //nếu chưa tồn tại tin liên quan
    else {
        $arr_tags = explode(',',$news['new_tags']);
        if(count($arr_tags) > 1) {
            $str_query = '';
            foreach($arr_tags as $tags) {
                $str_query .= ' + '.str_replace('"','',$tags).'';
            }
            $db_relate = new db_query('SELECT new_id,new_title,new_teaser,new_picture,new_cat_id,new_date,new_cat_id
                                        FROM news 
                                        WHERE new_cat_id = '.$news['new_cat_id'].' AND new_id != '.$news['new_id'].'
                                        AND (MATCH(new_title) AGAINST("'.trim($str_query).'" IN BOOLEAN MODE)) 
                                        ORDER BY rand(),new_date DESC
                                        LIMIT 10');   
            $list_relate = array();
            $relate_id = '';
            $i = 0;
            while($relate_item = mysql_fetch_assoc($db_relate->result)) {
                $i++;
                $list_relate[] = $relate_item;
                if($i == 1) {
                    $relate_id .= ''.$relate_item['new_id'].'';
                }else {
                    $relate_id .= ','.$relate_item['new_id'].'';
                }
            }
            unset($db_relate);
            $db_update_relate = new db_execute('UPDATE news SET new_relate = "'.$relate_id.'" WHERE new_id = '.$news['new_id']);
            if($db_update_relate->total == 1) {
                return $list_relate;
            } 
        }
        else {
           $db_relate = new db_query('SELECT new_id,new_title,new_teaser,new_picture,new_cat_id,new_date
                                        FROM news 
                                        WHERE new_cat_id = '.$news['new_cat_id'].' AND new_id != '.$news['new_id'].'
                                        ORDER BY new_date DESC
                                        LIMIT 10');   
            $list_relate = array();
            $relate_id = '';
            $i = 0;
            while($relate_item = mysql_fetch_assoc($db_relate->result)) {
                $i++;
                $list_relate[] = $relate_item;
                if($i == 1) {
                    $relate_id .= ''.$relate_item['new_id'].'';
                }else {
                    $relate_id .= ','.$relate_item['new_id'].'';
                }
            }
            unset($db_relate);
            $db_update_relate = new db_execute('UPDATE news SET new_relate = "'.$relate_id.'" WHERE new_id = '.$news['new_id']);
            if($db_update_relate->total == 1) {
                return $list_relate;
            } 
        } 
    }
}
?>